import { BaseEntity, Column, Entity, JoinColumn, OneToMany, ManyToOne, PrimaryGeneratedColumn, ManyToMany } from 'typeorm';
import { User } from './user';
import { Comment } from './comment';
import { Tag } from './tag'

@Entity({ name: 'articles' }) // 表头名
export class Article extends BaseEntity {
  @PrimaryGeneratedColumn()
  readonly id!: number;

  @Column()
  title!: string;

  @Column()
  content!: string;

  @Column()
  views!: number; // 文章看的人数

  @Column()
  create_time!: Date; // 文章的创建时间

  @Column()
  update_time!: Date; // 文章的更新时间

  @Column()
  is_delete!: number; // 假删除 0未删除，1已删除

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' }) // user_id 列作为外键来关联 User 表的主键
  user!: User;

  @ManyToMany(() => Tag, (tag) => tag.articles, {
    cascade: true
  })
  tags!: Tag[]


  @OneToMany(() => Comment, (comment) => comment.article)
  comments!: Comment[];
}
